package com.gzc.pm.logic
{
	import cn.skyclass.business.SessionManager;
	
	import com.gzc.pm.bean.DetectioncardBean;
	import com.gzc.pm.bean.DetsetbBean;
	import com.gzc.pm.bean.LineChartItemBean;
	import com.gzc.pm.bean.ToleranceBean;
	import com.gzc.pm.enum.StandardEnum;
	import com.gzc.pm.util.MyUtil;
	
	import mx.collections.ArrayCollection;

	/**
	 * 将检测数据detectioncardList,根据检测项配置信息detsetb中的key，解析生成key对应检测项的数据列表
	 */
	public class CollectionsParser
	{
		public function CollectionsParser(detectioncardList:ArrayCollection, detsetb:DetsetbBean, standard:uint)
		{
			_detectioncardList = detectioncardList;
			_detsetb = detsetb;
			_standard = standard;
			_toleranceData = SessionManager.current.getAttribute('toleranceData') as ToleranceLoader;
		}
		
		private var _detectioncardList:ArrayCollection;
		private var _detsetb:DetsetbBean;
		private var _standard:uint;
		private var _toleranceData:ToleranceLoader;
		
		public function parse():ArrayCollection{
			var key:String = _detsetb.key.toLowerCase();//检测项配置key值
			var tolerance:ToleranceBean = _toleranceData.getToleranceByEqucId(_detsetb.detsIdb);//公差值
			var myUtil:MyUtil = new MyUtil();
			
			var valueObj:Object =
				myUtil.calcCollectionsAvgAndStdD(_detectioncardList, key, _standard==StandardEnum.CONTROL.value);
			var avg:Number = valueObj.avg;
			var stdD:Number = valueObj.stdD;
			var level1:Number = (_standard==StandardEnum.CONTROL.value) ? valueObj.ucl : (tolerance?tolerance.usl:NaN);
			var level2:Number = (_standard==StandardEnum.CONTROL.value) ? valueObj.lcl: (tolerance?tolerance.lsl:NaN);
			
			var ds:ArrayCollection = new ArrayCollection();
			for each(var collec:DetectioncardBean in _detectioncardList){
				var chartItem:LineChartItemBean =
					new LineChartItemBean(collec.productId, Number(collec[key]), avg, stdD, level1, level2,
						_standard,
						key, _detsetb.tempv, collec.testDate);
				ds.addItem(chartItem);
			}
			if(_standard==StandardEnum.CONTROL.value){
				myUtil.checkByControlRules(ds);
			}else if(_standard==StandardEnum.TOLERANCE.value){
				//TODO:临时
//				myUtil.checkByToleranceRules(ds);
			}
			
			return ds;
		}
		
	}
}